<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<form class="layui-form" action="" lay-filter="orderForm">
    <div class="layui-form-item" style="margin-top:10px">
        <div class="layui-inline">
            <label class="layui-form-label">部门名称：</label>
            <div class="layui-input-block">
                <select class="layui-form-select" id="partName" lay-filter="partFilter">
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">员工姓名：</label>
            <div class="layui-input-block">
                <select class="layui-form-select" id="employeeTrueName" lay-filter="employeeFilter">
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">客户姓名：</label>
            <div class="layui-input-block">
                <select class="layui-form-select" id="clientName" lay-filter="clientFilter">
                </select>
            </div>
        </div>
        <button type="button" id="formSubmit" class="layui-inline layui-btn" lay-event="query">查询</button>
        <button type="button" id="query1" class="layui-inline layui-btn layui-btn-warm" lay-event="query1">查询已完成订单</button>
        <button type="button" id="query2" class="layui-inline layui-btn layui-btn-warm" lay-event="query2">查询废弃订单</button>
        <button type="button" id="addOrder" class="layui-inline layui-btn layui-btn-warm" lay-event="add">添加订单</button>
    </div>
</form>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<style type="text/css">
    .layui-form-select {
        width: 150px;
    }
    .layui-form-label {
        width: 70px;
    }
</style>


<script src="https://www.layuicdn.com/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.min.js"></script>

<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','form','jquery','layer'], function() {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;
        table.render({
            id : 'orderTable'
            ,elem: '#test'
            ,url:'/firstWeb/order/queryAll'
            ,title: '用户数据表'
            ,where: {}
            ,cols: [[
                {field:'id', title:'订单编号'}
                ,{field:'client', title:'客户姓名', templet: function(data) {
                    return data.client.name;
                 }}
                ,{field:'employee', title:'下单员工', templet: function(data) {
                    return data.employee.trueName;
                 }}
                ,{field:'time', title:'下单时间'}
                ,{field: 'is', title:'订单状态', templet: function(d) {
                    if(0 == d.is) {
                        return "已废弃";
                    } else if(1 == d.is){
                        return "未完成";
                    } else {
                        return "已完成";
                    }
                 }}
                ,{fixed: 'right', title:'订单详情', toolbar: '#barDemo'}
            ]]
            ,page: true // 默认一页十条
            ,limit:10
            ,limits: [1,5,10,20]
        });


        //页面加载时向下拉选框中加载数据
        $(document).ready(function() {
            axios.post('/firstWeb/part/queryName').then(function (response) {
                var role = document.getElementById("partName");
                var data = response.data;
                for(var i = 0; i < data.length; i++) {
                    var value = Object.keys(data);
                    var label = Object.values(data);
                    var option = document.createElement("option");  // 创建添加option属性
                    option.setAttribute("value", value[i]); 	    // 给option的value添加值
                    option.innerText = label[i];    			    // 打印option对应的纯文本
                    role.appendChild(option);  					    // 给select添加option子标签
                }
                form.render("select");
            }).catch(function (error) {
                console.log(error);
            });
        });

        //部门名称select监听器
        form.on('select(partFilter)', function(data) {
            $("#employeeTrueName").empty();
            var partName = {"name": data.elem[data.elem.selectedIndex].text};
            axios.post('/firstWeb/employee/queryTrueName', partName).then(function(response) {
                var role = document.getElementById("employeeTrueName");
                var data = response.data;
                for(var i = 0; i < data.length; i++) {
                    var value = Object.keys(data);
                    var label = Object.values(data);
                    var option = document.createElement("option");  // 创建添加option属性
                    option.setAttribute("value", value[i]); 	    // 给option的value添加值
                    option.innerText = label[i];    			    // 打印option对应的纯文本
                    role.appendChild(option);  					    // 给select添加option子标签
                }
                form.render("select");
            }).catch(function (error) {
                console.log(error);
            });
        });

        //员工姓名select监听器
        form.on('select(employeeFilter)', function(data) {
            $("#clientName").empty();
            var employeeTrueName = {"trueName": data.elem[data.elem.selectedIndex].text};
            console.log(employeeTrueName);
            axios.post('/firstWeb/client/queryName', employeeTrueName).then(function(response) {
                var role = document.getElementById("clientName");
                var data = response.data;
                console.log(response);
                for(var i = 0; i < data.length; i++) {
                    var value = Object.keys(data);
                    var label = Object.values(data);
                    var option = document.createElement("option");  // 创建添加option属性
                    option.setAttribute("value", value[i]); 	    // 给option的value添加值
                    option.innerText = label[i];    			    // 打印option对应的纯文本
                    role.appendChild(option);  					    // 给select添加option子标签
                }
                form.render("select");
            }).catch(function (error) {
                console.log(error);
            });
        });

        //查询点击事件
        $("#formSubmit").click(function() {
            tableReload();
        });

        //
        $("#addOrder").click(function() {
            layer.open({
                type: 2 //此处以iframe举例
                ,title: '添加订单'
                ,area: ['390px', '260px']
                ,shade: 0
                ,maxmin: true
                ,content: 'add.html'
                ,btn: ['提交', '关闭']
                ,yes: function(index) {
                    var data = window['layui-layer-iframe' + index].getValue();
                    if (!data.name) {
                        layer.msg("部门名称不能为空");
                        return false;
                    } else if (!data.name) {
                        layer.msg("员工姓名不能为空");
                        return false;
                    } else if (!data.name) {
                        layer.msg("客户姓名不能为空");
                        return false;
                    }
                    //发送新增请求 get/post √
                    axios.post('/firstWeb/order/saveOrder', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "1") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("新增成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                }
                ,no: function(index){//5
                    layer.close(index);
                }
            });
        });









        //重新加载表格数据
        function tableReload() {
            var data = form.val('orderForm');
            table.reload('orderTable', {
                where : data
                ,page: {
                    curr: 1
                }
            });
        }



        $("#add").click(function() {
            layer.open({
                type: 2 //此处以iframe举例
                ,title: '新增页面'
                ,area: ['390px', '260px']
                ,shade: 0
                ,maxmin: true
                ,content: 'dialog.html'
                ,btn: ['提交', '关闭']
                ,yes: function(index){
                    var data = window['layui-layer-iframe' + index].getValue();
                    if (!data.name) {
                        layer.msg("部门名称不能为空");
                        return false;
                    }
                    //发送新增请求 get/post √
                    axios.post('/firstWeb/part/savePart', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "1") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("新增成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                }
                ,no: function(index){//5
                    layer.close(index);
                }
            });
        })


        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            console.log(obj);
            if(obj.event === 'del'){//删除
                layer.confirm('真的删除行么', function(index){
                    axios.post('/firstWeb/part/updateState', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "1") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("删除成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit') {//编辑
                layer.open({
                    type: 2 //此处以iframe举例
                    ,title: '修改页面'
                    ,area: ['390px', '260px']
                    ,shade: 0
                    ,maxmin: true
                    ,content: 'dialog.html'
                    ,btn: ['提交', '关闭']
                    ,yes: function(index){
                        var data = window['layui-layer-iframe' + index].getValue();
                        data.id = obj.data.id;
                        if (!data.name) {
                            layer.msg("部门名称不能为空");
                            return false;
                        }
                        //发送新增请求 get/post √
                        axios.post('/firstWeb/part/update', data).then(function(response) {
                            //post请求执行成功的回调
                            if (response.data.status == "1") {
                                layer.close(index);
                                //刷新表格
                                tableReload();
                                layer.msg("更新成功");
                            } else {
                                layer.msg(response.data.msg)
                            }
                        }).catch(function (error) {
                            console.log(error);
                        });
                    }
                    ,no: function(index){//5
                        layer.close(index);
                    }
                });
            }
        });
    });
</script>

</body>
</html>